package com.movie.app.controller;

import java.util.Map;

import com.movie.app.model.Admin;
import com.movie.app.model.ResponseCode;
import com.movie.app.utils.Jwt;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin/")
public class AdminController {
    @Autowired
	private MongoTemplate mongoTemplate;

    @Autowired
    private Jwt jwt;
    /**
     * 对密码进行加密
     * @param password
     * @return
     */
    public static String encode(String password) {
        BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder();
        String hashPass = bcryptPasswordEncoder.encode(password);
        return hashPass;
    }
    /**
     * 对原密码和已加密的密码进行匹配，判断是否相等
     * @param password
     * @param encodedPassword
     * @return
     */
    public static boolean match(String password,String encodedPassword) {
        BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder();
        boolean result = bcryptPasswordEncoder.matches(password, encodedPassword);
        return result;
    }

    @PostMapping("/")
    public ResponseCode add(@RequestBody Admin admin, @RequestHeader Map<String, String> headers){
        String jwtString;
        ResponseCode rcode = new ResponseCode();

		try {
			jwtString = headers.get("jwt");
		} catch (Exception e) {
			rcode.setCode(0);
			return rcode;
		}
		if (!jwtString.equals("yousaiteng")) {
            rcode.setCode(0);
            rcode.setContent("error jwt");
            return rcode;
        }

        AdminFilter filter = new AdminFilter();
        rcode = filter.addValid(admin);

		if (rcode.getCode()==0) {
			return rcode;
		}

        admin.setPassword(encode(admin.getPassword()));
        mongoTemplate.save(admin);
        return rcode;
    }

    @PostMapping("/login/")
    public ResponseCode login(@RequestBody Admin admin){
        String name     = admin.getName();
        String password = admin.getPassword();

        Query query = new Query();
        query.addCriteria(Criteria.where("name").is(name));
        Admin user  = mongoTemplate.findOne(query, Admin.class);

        ResponseCode rCode = new ResponseCode();

        if (match(password, user.getPassword())){
            String token = jwt.genToken(user.getId());
            rCode.setCode(1);
            rCode.setContent(token);
            return rCode;
        }
        rCode.setCode(0);
        rCode.setContent("error name or password");;
        return rCode;
    }

}